/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.deloitte.cvapp.dataaccess;

import ie.deloitte.cvapp.domain.Employee;
import ie.deloitte.cvapp.domain.EmployeeHasSkill;
import ie.deloitte.cvapp.domain.Skills;
import ie.deloitte.cvapp.form.CVSearchCriteria;
import ie.deloitte.cvapp.form.EmployeeBeanForm;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author damorales
 */
@Repository("employeeDAO")
@Transactional
public class EmployeeDAOImpl implements EmployeeDAO {

	@Autowired
	private SessionFactory sessionFactory;

	private Session currentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void save(Employee e) {
		currentSession().saveOrUpdate(e);
	}

	public List<Employee> search(CVSearchCriteria cVSearchCriteria) {
		System.out.println("FROM THE DAO");
		Query namedQuery = currentSession().getNamedQuery("Employee.findByNameSurnameRoleEmailSkills");

		namedQuery.setParameter("name", cVSearchCriteria.getName())
		          .setParameter("surname", cVSearchCriteria.getSurname())
				  .setParameter("name2","%" + cVSearchCriteria.getName() +"%")
	    		  .setParameter("surname2","%" + cVSearchCriteria.getSurname() +"%")				  
				  .setParameter("email", cVSearchCriteria.getEmail());
				  //.setParameter("skills", cVSearchCriteria.getSkillsMatrix());
		//.setParameter("roleproposed","%" + cVSearchCriteria.getRole() +"%")
		//namedQuery.setParameter("surname","%" + cVSearchCriteria.getSurname() +"%");
		
		List list = namedQuery.list();
		
		for (Object emp: list){
			System.out.println(emp.toString());
		}
		
		return list;
	}
	
	public Employee getById(String id) {
		Query namedQuery = currentSession()
				.getNamedQuery("Employee.findByIdEmployee");
		namedQuery.setParameter("idEmployee", Integer.valueOf(id));
		List list = namedQuery.list();
		Employee e = new Employee();
		if (!list.isEmpty()) {
			e = (Employee) list.get(0);
		}
		return e;
	}

	public EmployeeBeanForm getEmployeeBeanFormById(String id){
		Employee employee = getById(id);
		EmployeeBeanForm ebf = new EmployeeBeanForm();
		
		ebf.setCreationdate(employee.getCreationdate());
		ebf.setName(employee.getName());
		ebf.setSurname(employee.getSurname());
		ebf.setEmail(employee.getEmail());
		ebf.setPrimarytelephone(employee.getPrimarytelephone());
		ebf.setSecondarytelepohne(employee.getSecondarytelepohne());
		ebf.setProfile(employee.getProfile());
		
		return ebf;
	}
	
	public void update(Employee e) {
		currentSession().update(e);
	}

	public List<Employee> getAll() {
		Query namedQuery = currentSession().getNamedQuery("Employee.findAll");
		List<Employee> list = namedQuery.list();
		for (Employee employee : list) {
			loadSkills(employee);
		}

		return list;
	}

	private void loadSkills(Employee employee) {
		Set<EmployeeHasSkill> skills = employee.getSkills();
		for (EmployeeHasSkill employeeSkill : skills) {
			Skills skill = employeeSkill.getSkill();
			skill.getName();
		}
	}
}
